/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.view;

import java.util.ArrayList;
import java.util.List;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import org.colswe.solestac.VO.RequestVO;
import org.colswe.solestac.VO.StudentVO;
import org.colswe.solestac.control.RequestControl;
import org.colswe.solestac.control.StudentControl;

/**
 *
 * @author laarguelless
 * @version 1.1
 */
public class searchRequest extends javax.swing.JPanel {

    /**
     * Creates new form searchRequest
     */
    private final String firstOption = "Seleccione uno...";

    public searchRequest() {
        initComponents();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        idLbl = new javax.swing.JLabel();
        idTF = new javax.swing.JTextField();
        programLbl = new javax.swing.JLabel();
        programCB = new javax.swing.JComboBox();
        stateLbl = new javax.swing.JLabel();
        intanceCB = new javax.swing.JComboBox();
        typeLbl = new javax.swing.JLabel();
        studentLbl = new javax.swing.JLabel();
        studentTF = new javax.swing.JTextField();
        periodLbl = new javax.swing.JLabel();
        periodTF = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        requestTable = new javax.swing.JTable();
        searchButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        List<String> typesL = new RequestControl().getAllRequestTypes();
        String[] types = new String[typesL.size()+1];
        types[0] = firstOption;
        for(int i = 0; i  < typesL.size(); i++){
            types[i+1] = typesL.get(i);
        }
        typeList = new javax.swing.JList(types);

        setBackground(new java.awt.Color(255, 255, 255));

        idLbl.setText("Número de solicitud");

        idTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                idTFActionPerformed(evt);
            }
        });

        programLbl.setText("Programa curricular");

        List<String> programsL = new StudentControl().getAllPrograms();
        String[] programs = new String[programsL.size()+1];
        programs[0] = firstOption;
        for (int i = 0; i < programsL.size(); i++){
            programs[i+1]  = programsL.get(i);
        }
        programCB.setModel(new javax.swing.DefaultComboBoxModel(programs));

        stateLbl.setText("Estado");

        List<String> intancesL = new RequestControl().getAllInstances();
        String[] instances = new String[intancesL.size()+1];
        instances[0] = firstOption;
        for (int i = 0; i < intancesL.size(); i++){
            instances[i+1] = intancesL.get(i);
        }
        intanceCB.setModel(new javax.swing.DefaultComboBoxModel(instances));

        typeLbl.setText("Tipo de Solicitud");

        studentLbl.setText("Estudiante");

        periodLbl.setText("Periodo académico");

        periodTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                periodTFActionPerformed(evt);
            }
        });

        requestTable.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 16)); // NOI18N
        requestTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Tipo", "Fecha", "Periodo", "Instancia Actual "
            }
        ));
        requestTable.setDragEnabled(true);
        jScrollPane2.setViewportView(requestTable);

        searchButton.setText("Buscar");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        typeList.setSelectedIndex(0);
        typeList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        typeList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        typeList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                typeListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(typeList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 636, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 44, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(periodLbl)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(studentLbl)
                                .addGap(119, 119, 119)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(periodTF, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(studentTF, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(idLbl)
                                    .addComponent(programLbl)
                                    .addComponent(stateLbl)
                                    .addComponent(typeLbl))
                                .addGap(51, 51, 51)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(idTF, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 382, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(intanceCB, javax.swing.GroupLayout.Alignment.LEADING, 0, 306, Short.MAX_VALUE)
                                        .addComponent(programCB, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                        .addContainerGap())))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(126, 126, 126))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idLbl)
                    .addComponent(idTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(programLbl)
                    .addComponent(programCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(stateLbl))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(intanceCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(typeLbl))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(studentLbl)
                            .addComponent(studentTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(periodTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(periodLbl)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(76, 76, 76)
                        .addComponent(searchButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void idTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_idTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_idTFActionPerformed

    private void periodTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_periodTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_periodTFActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        RequestVO request = new RequestVO();
        StudentVO student = new StudentVO();

        request.setId(idTF.getText());

        if (programCB.getSelectedItem().toString().equals(firstOption)) {
            request.setProgram("");
        } else {
            request.setProgram(programCB.getSelectedItem().toString());
        }
        if (intanceCB.getSelectedItem().toString().equals(firstOption)) {
            request.setInstance("");
        } else {
            request.setInstance(intanceCB.getSelectedItem().toString());
        }

        student.setDocument(studentTF.getText());
        request.setAcademicPeriod(periodTF.getText());

        RequestControl requestControl = new RequestControl();
        // int result = requestControl.verifyFilters(request, student.getDocument());
        //System.out.println(result);

        boolean notType = false;
        int[] indices = typeList.getSelectedIndices();

        List<String> types = new ArrayList<String>();
        for (int i = 0; i < indices.length; i++) {
            int index = indices[i];

            if (typeList.getModel().getElementAt(index).equals(firstOption)) {
                notType = true;
            } else {
                types.add((String) typeList.getModel().getElementAt(index));
            }
       }

        List<RequestVO> searchByFilter = new ArrayList<RequestVO>();

        if (notType) {
            searchByFilter = requestControl.searchByFilter(request, student.getDocument(), new ArrayList<String>());
        } else {
            searchByFilter = requestControl.searchByFilter(request, student.getDocument(), types);
        }
        clearTable();
        for (RequestVO requestVO : searchByFilter) {
            setTable(searchByFilter);
        }

    }//GEN-LAST:event_searchButtonActionPerformed
        

    private void clearTable() {
        Object[][] tableModel = {};
        requestTable.setModel(new DefaultTableModel(tableModel, new String[]{"Id", "Tipo", "Fecha", "Periodo", "Instancia Actual"}));
    }

    private void setTable(List<RequestVO> requests) {
        clearTable();
        if (requests != null && !requests.isEmpty()) {
            int rows = requests.size();
            Object[][] tableModel = new Object[rows][5];
            for (int i = 0; i < rows; i++) {
                tableModel[i][0] = requests.get(i).getId();
                tableModel[i][1] = requests.get(i).getRequestType();
                tableModel[i][2] = requests.get(i).getDate();
                tableModel[i][3] = requests.get(i).getAcademicPeriod();
                tableModel[i][4] = requests.get(i).getInstance();
            }

            requestTable.setModel(new DefaultTableModel(tableModel, new String[]{"Id", "Tipo", "Fecha", "Periodo", "Instancia Actual"}));

        }
    }

    private void typeListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_typeListValueChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_typeListValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel idLbl;
    private javax.swing.JTextField idTF;
    private javax.swing.JComboBox intanceCB;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel periodLbl;
    private javax.swing.JTextField periodTF;
    private javax.swing.JComboBox programCB;
    private javax.swing.JLabel programLbl;
    private javax.swing.JTable requestTable;
    private javax.swing.JButton searchButton;
    private javax.swing.JLabel stateLbl;
    private javax.swing.JLabel studentLbl;
    private javax.swing.JTextField studentTF;
    private javax.swing.JLabel typeLbl;
    private javax.swing.JList typeList;
    // End of variables declaration//GEN-END:variables
}
